package com.chengqs.framework.context.support;

import com.chengqs.framework.beans.factory.support.BeanDefinitionReader;
import com.chengqs.framework.beans.factory.support.BeanDefinitionRegistry;
import com.chengqs.framework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * ApplicationContext接口的子实现类，用于立即加载
 */
public abstract class AbstractApplicationContext implements ApplicationContext {

    // 声明解析器
    protected BeanDefinitionReader beanDefinitionReader;

    // 定义用于存储bean对象的map容器
    protected Map<String, Object> singletonObjects = new HashMap<>();

    // 声明配置文件路径的变量
    protected String configLocation;

    @Override
    public void refresh() throws Exception {
        // 加载beanBeanDefinition对象
        beanDefinitionReader.loadBeanDefinitions(configLocation);
        // 初始化bean
        finishBeanInitialization();
    }

    private void finishBeanInitialization() throws Exception {
        // 获取注册表对象
        BeanDefinitionRegistry register = beanDefinitionReader.getRegister();

        // 获取BeanDefinition对象
        String[] beanNames = register.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            // 进行bean的初始化
            getBean(beanName);
        }
    }
}
